//
//  UITextView+ZUtilities.m
//  WeClubs
//
//  Created by upintech on 17/7/6.
//  Copyright © 2017年 Zed. All rights reserved.
//

#import "UITextView+ZUtilities.h"
#include <objc/runtime.h>
#import "Masonry.h"

static const void *s_hdfTextViewPlaceholderLabelKey = "s_hdfTextViewPlaceholderLabelKey";
static const void *s_hdfTextViewPlaceholderTextKey = "s_hdfTextViewPlaceholderTextKey";

@interface UIApplication (ZUtilities)

@end

@implementation UIApplication (ZUtilities)

- (void)hdf_placehoderTextChange:(NSNotification *)nofitication {
 
    UITextView *textView = nofitication.object;
    if ([textView isKindOfClass:[UITextView class]]) {
        if (!textView.text) {
            textView.hdf_placeholderLabel.text = @"";
        } else {
            textView.hdf_placeholderLabel.text = textView.hdf_placeholder;
        }
    }
}

@end

@interface UITextView (HDFPlaceholderTextView)

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation UITextView (HDFPlaceholderTextView)

- (void)setPlaceholderLabel:(UILabel *)placeholderLabel {
    objc_setAssociatedObject(self,
                             s_hdfTextViewPlaceholderLabelKey,
                             placeholderLabel,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UILabel *)placeholderLabel {
    UILabel *label = objc_getAssociatedObject(self, s_hdfTextViewPlaceholderLabelKey);
    
    if (label == nil || ![label isKindOfClass:[UILabel class]]) {
        label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = self.font;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = SEPARATOR_BG_COLOR;
        [self addSubview:label];
        
        __weak typeof(self) weakSelf = self;
        self.placeholderLabel = label;
        CGFloat left = 7;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(weakSelf).insets(UIEdgeInsetsMake(7.5, left, 0, 0));
        }];
        label.enabled = NO;
        
        [[NSNotificationCenter defaultCenter] addObserver: self
                                selector:@selector(hdf_placehoderTextChange:)
                                    name:UITextViewTextDidChangeNotification
                                  object:nil];
    }
    
    return label;
}

@end

@implementation UITextView (HDFTextView)

- (void)hdf_placehoderTextChange:(NSNotification *)notification {
    
        self.placeholderLabel.text = self.hdf_placeholder;
    

}

- (UILabel *)hdf_placeholderLabel {
    return self.placeholderLabel;
}

- (void)setHdf_placeholder:(NSString *)hdf_placeholder {
    if (hdf_placeholder == nil) {
        objc_setAssociatedObject(self, s_hdfTextViewPlaceholderLabelKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self.placeholderLabel removeFromSuperview];
        return;
    }
    
    objc_setAssociatedObject(self,
                             s_hdfTextViewPlaceholderTextKey,
                             hdf_placeholder,
                             OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    if (self.text == nil) {
        self.placeholderLabel.text = @"";
    } else {
        self.placeholderLabel.text = hdf_placeholder;
    }
}

- (NSString *)hdf_placeholder {
    return objc_getAssociatedObject(self, s_hdfTextViewPlaceholderTextKey);
}

- (void)setHdf_placeholderColor:(UIColor *)hdf_placeholderColor {
    self.placeholderLabel.textColor = hdf_placeholderColor;
}

- (UIColor *)hdf_placeholderColor {
    return self.placeholderLabel.textColor;
}

- (void)setHdf_placeholderFont:(UIFont *)hdf_placeholderFont {
    self.placeholderLabel.font = hdf_placeholderFont;
}

- (UIFont *)hdf_placeholderFont {
    return self.placeholderLabel.font;
}

@end
